<template>
  <div class="container">
    <map-route :route-info="routeInfo" v-if="routeInfo.startLat"></map-route>
  </div>
</template>
<script>
import gcoord from 'gcoord'
let plugin = requirePlugin("mapPlugin")
export default {
  data () {
    return {
      routeInfo: {},
      hosLat: "",
      hosLng: "",
      hosName: "",
      myLat: 0,
      myLng: 0,
    }

  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的位置"
    });
    if (options) {
      // console.log(options)
      // console.log(gcoord)
      this.hosLat = options.hosLat;
      this.hosLng = options.hosLng
      this.hosName = options.hosName
    }
    let that = this
    // let obj = JSON.parse(wx.getStorageSync('province_info'))
    wx.getLocation({
      type: 'gcj02', //默认为 wgs84 返回 gps 坐标，gcj02 返回可用于wx.openLocation的坐标,
      success: res => {
        const latitude = res.latitude
        const longitude = res.longitude
        var result = gcoord.transform(
          [that.hosLng, that.hosLat],    // 经纬度坐标
          gcoord.GCJ02,               // 当前坐标系
          gcoord.GCJ02                 // 目标坐标系
        );
        var result_mine = gcoord.transform(
          [latitude, longitude],    // 经纬度坐标
          gcoord.GCJ02,               // 当前坐标系
          gcoord.GCJ02                 // 目标坐标系
        );

        let routeInfo = {
          startLat: latitude,    //纬度 选填
          startLng: longitude,    //经度 选填
          startName: "我的位置",
          endLat: result[1],    // 终点纬度必传
          endLng: result[0],  //终点经度 必传
          endName: that.hosName,  //终点名称 必传
          mode: "car"  //算路方式 选填
        }
        console.log(routeInfo);
        
        if (routeInfo.startLat == null) {
          return false;
        }
        that.routeInfo = routeInfo;
        // console.log(that.routeInfo)
      },
      fail: () => {
        wx.showToast({
          title: '获取当前位置失败', //提示的内容,
          icon: 'none', //图标,
          duration: 1500, //延迟时间,
          mask: true, //显示透明蒙层，防止触摸穿透,
          success: res => {

          }
        });
      }
    });
  },
  onShow () {

  },

};


</script>
<style scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>

     